package com.blog.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blog.common.util.jstree.BuildTree;
import com.blog.common.util.jstree.Tree;
import com.blog.dao.sys.RoleDao;
import com.blog.entity.sys.Role;
import com.blog.service.sys.RoleService;
 

@Service
public class RoleServiceImp  implements RoleService{
	
	@Autowired
	private RoleDao dao;
	
	@Override
	public List<Role> listRole(Integer userId) {
		return dao.listRole(userId);
	}

	@Override
	public Integer delectUserAndRole(Integer userIds) {
		return dao.delectUserAndRole(userIds);
	}

 
	@Override
	public Tree<Role> listRoleTree() {
		List<Tree<Role>> trees = new ArrayList<Tree<Role>>();
		List<Role> sysRoles = dao.listRoleTree(new HashMap<String,Object>(16));
		for(Role role : sysRoles)
		{
			//封装tree
			Tree<Role> tree=new Tree<Role>();
			tree.setId(role.getId().toString());
			tree.setParentId(role.getPid().toString());
			tree.setText(role.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", true);
			tree.setState(state);
			trees.add(tree);
		}
		// 默认顶级菜单为０，根据数据库实际情况调整
		Tree<Role> t = BuildTree.build(trees);
		return t;
	}

	@Override
	public List<Role> selectRoleByUserId(Integer id) {
		return dao.selectRoleByUserId(id);
	}

	@Override
	public List<Role> listRoles(Map<String, Object> map) {
		return dao.listRoleTree(map);
	}

	@Override
	public Integer saveRole(Role role) {
	 
		return dao.saveRole(role);
	}

	@Override
	public Integer updateRole(Role role) {
		 
		return dao.updateRole(role);
	}

	@Override
	public Role selectRoleByRoleId(Integer roleId) {
	 
		return dao.selectRoleByRoleId(roleId);
	}

	@Override
	public Integer delectRoleById(Integer roleId) {
		return dao.delectRoleById(roleId);
	}

	
	
 

}
